package com.demo;

/**
 * @ClassName: Singleton01
 * @Description: 懒汉式
 * @Date: 2020/11/19 10:03
 */
public class Singleton01 {
    //用的时候在创建对象
    private static Singleton01 singleton01;
    private Singleton01(){}
    //进行判断
    public static Singleton01 getInstance(){
        //最外层判断是否为空用来提高效率
        if(singleton01==null){
            synchronized (Singleton01.class){
                //加上同步锁提高线程安全
                if(singleton01==null){
                    singleton01=new Singleton01();
                }
            }
        }
        return singleton01;
    }

    public static void main(String[] args) {
        System.out.println(getInstance());
    }
}
